﻿<script runat="template">
private Regex cleanRegEx = new Regex(@"\s+|_|-|\.", RegexOptions.Compiled);
private Regex cleanID = new Regex(@"(_ID|_id|_Id|\.ID|\.id|\.Id|ID|Id)", RegexOptions.Compiled);

//清除字符串中的分隔符以及第一个分隔符前的字符串长度小于等于prefixLength的前缀
public string ClearDivideMarkAndPrefix(string name,string mark,bool prefix,int prefixLength)
{
    string strResult=name;
    //处理表前缀
    if (prefix)
    {
        int mIndex=name.IndexOf(mark);
        if (mIndex<=prefixLength)
            strResult=name.Remove(0,mIndex+mark.Length);
    }
    //if (mark!=null && mark.Length>0)
        //strResult=GetFirstUpStr(strResult,mark);
    return strResult;
}
//受字母大写且mark分隔符后第一个字母大写
    public string GetFirstUpStr(string strValue,string mark)
    {
        string m_Str=strValue.ToLower();
        System.Text.StringBuilder mResult=new System.Text.StringBuilder("");
        int mLen=m_Str.Length;
        int j=-1;
        j=m_Str.IndexOf(mark);
        while (j>0)
        {
            mResult.Append(m_Str.Substring(0,1).ToUpper());
            mResult.Append(m_Str.Substring(1,j-1));
            m_Str=m_Str.Remove(0,j+mark.Length);
            j=m_Str.IndexOf(mark);
        }
        if (m_Str.Length>0)
        {
            mResult.Append(m_Str.Substring(0,1).ToUpper());
            mResult.Append(m_Str.Substring(1,m_Str.Length-1));
            }
        return mResult.ToString();
    }
       /// <summary>
        /// 获取c#变量类型
        /// </summary>
        /// <param name="column">列</param>
        /// <returns>c#变量类型</returns>
        public string GetType(ColumnSchema column)
        {
            if (column.Name.EndsWith("TypeCode")) return column.Name;
            
            switch (column.DataType)
            {
                case DbType.AnsiString: return "string";
                case DbType.AnsiStringFixedLength: return "string";
                case DbType.Binary: return "byte[]";
                case DbType.Boolean: return "bool";
                case DbType.Byte: return "byte";
                case DbType.Currency: return "decimal";
                case DbType.Date: return "DateTime";
                case DbType.DateTime: return "DateTime";
                case DbType.Decimal: return "decimal";
                case DbType.Double: return "double";
                case DbType.Guid: return "Guid";
                case DbType.Int16: return "short";
                case DbType.Int32: return "int";
                case DbType.Int64: return "long";
                case DbType.Object: return "object";
                case DbType.SByte: return "sbyte";
                case DbType.Single: return "float";
                case DbType.String: return "string";
                case DbType.StringFixedLength: return "string";
                case DbType.Time: return "TimeSpan";
                case DbType.UInt16: return "ushort";
                case DbType.UInt32: return "uint";
                case DbType.UInt64: return "ulong";
                case DbType.VarNumeric: return "decimal";
                case DbType.Xml: return "int"; //约定 xml 为枚举
                default:
                {
                    return "__UNKNOWN__" + column.NativeType;
                }
            }
        }
        
        /// <summary>
        /// 获取c#变量类型是否为枚举
        /// </summary>
        /// <param name="column">列</param>
        /// <returns>c#变量类型</returns>
        public bool IsEnumType(ColumnSchema column)
        {
            if (column.DataType == DbType.Xml) return true;
            return false;
        }
        
        /// <summary>
        /// 获取c#变量类型默认值
        /// </summary>
        /// <param name="column">列</param>
        /// <returns>c#变量类型默认值</returns>
        public string GetDefaultValue(ColumnSchema column)
        {
            if (column.Name.EndsWith("TypeCode")) return column.Name;
            
            switch (column.DataType)
            {
                case DbType.AnsiString: return "\"\"";
                case DbType.AnsiStringFixedLength: return "\"\"";
                case DbType.Binary: return "null";
                case DbType.Boolean: return "false";
                case DbType.Byte: return "0";
                case DbType.Currency: return "0";
                case DbType.Date: return "DateTime.Parse(\"1900-1-1\")";
                case DbType.DateTime: return "DateTime.Parse(\"1900-1-1\")";
                case DbType.Decimal: return "0";
                case DbType.Double: return "0";
                case DbType.Guid: return "Guid.NewGuid().ToString()";
                case DbType.Int16: return "0";
                case DbType.Int32: return "0";
                case DbType.Int64: return "0";
                case DbType.Object: return "\"\"";
                case DbType.SByte: return "0";
                case DbType.Single: return "0";
                case DbType.String: return "\"\"";
                case DbType.StringFixedLength: return "";
                case DbType.Time: return "DateTime.Parse(\"1900-1-1\")";
                case DbType.UInt16: return "0";
                case DbType.UInt32: return "0";
                case DbType.UInt64: return "0";
                case DbType.VarNumeric: return "0";
                default:
                {
                    return "__UNKNOWN__" + column.NativeType;
                }
            }
        }
        /// <summary>
        /// 判断列是否为值类型
        /// </summary>
        /// <param name="column">列</param>
        /// <returns>c#变量类型</returns>
        public bool ColIsValType(ColumnSchema column)
        {           
            switch (column.DataType)
            {
                case DbType.AnsiString: return false;
                case DbType.AnsiStringFixedLength: return false;
                case DbType.Binary: return false;
                case DbType.Boolean: return false;
                case DbType.Byte: return true;
                case DbType.Currency: return true;
                case DbType.Date: return true;
                case DbType.DateTime: return true;
                case DbType.Decimal: return true;
                case DbType.Double: return true;
                case DbType.Guid: return false;
                case DbType.Int16: return true;
                case DbType.Int32: return true;
                case DbType.Int64: return true;
                case DbType.Object: return false;
                case DbType.SByte: return false;
                case DbType.Single: return true;
                case DbType.String: return false;
                case DbType.StringFixedLength: return false;
                case DbType.Time: return false;
                case DbType.UInt16: return true;
                case DbType.UInt32: return true;
                case DbType.UInt64: return true;
                case DbType.VarNumeric: return true;
                default:
                {
                    return false;
                }
            }
        }
        /// <summary>
        /// 判断列是否为日期类型
        /// </summary>
        /// <param name="column">列</param>
        /// <returns>c#变量类型</returns>
        public bool ColIsDateType(ColumnSchema column)
        {           
            switch (column.DataType)
            {               
                case DbType.Date: return true;
                case DbType.DateTime: return true;               
                default:
                {
                    return false;
                }
            }
        }
        
        /// <summary>
        /// 首字母小写
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string FirstLetterToLower(string str)
        {
         if (str == null)
          return null;
         if (str.Length > 1)
          return char.ToLower(str[0]) + str.Substring(1);
         return str;
        }
        
        /// <summary>
        /// 单词变成单数形式
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public string ToSingular(string word)
        {
            Regex plural1 = new Regex("(?<keep>[^aeiou])ies$");
            Regex plural2 = new Regex("(?<keep>[aeiou]y)s$");
            Regex plural3 = new Regex("(?<keep>[sxzh])es$");
            Regex plural4 = new Regex("(?<keep>[^sxzhyu])s$");
 
            if (plural1.IsMatch(word))
                return plural1.Replace(word, "${keep}y");
            else if (plural2.IsMatch(word))
                return plural2.Replace(word, "${keep}");
            else if (plural3.IsMatch(word))
                return plural3.Replace(word, "${keep}");
            else if (plural4.IsMatch(word))
                return plural4.Replace(word, "${keep}");
 
            return word;
        }
        /// <summary>
        /// 单词变成复数形式
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public string ToPlural(string word)
        {
            Regex plural1 = new Regex("(?<keep>[^aeiou])y$");
            Regex plural2 = new Regex("(?<keep>[aeiou]y)$");
            Regex plural3 = new Regex("(?<keep>[sxzh])$");
            Regex plural4 = new Regex("(?<keep>[^sxzhy])$");
 
            if (plural1.IsMatch(word))
                return plural1.Replace(word, "${keep}ies");
            else if (plural2.IsMatch(word))
                return plural2.Replace(word, "${keep}s");
            else if (plural3.IsMatch(word))
                return plural3.Replace(word, "${keep}es");
            else if (plural4.IsMatch(word))
                return plural4.Replace(word, "${keep}s");
 
            return word;
        }
        /// <summary>
        /// 小写
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public string ToLower(string word)
        { 
            return word.ToLower();
        }

</script>